package com.example.mybiadushoujiweishi.activity.acivitys.utils;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;


import com.example.mybiadushoujiweishi.BuildConfig;
import com.example.mybiadushoujiweishi.R;
import com.example.mybiadushoujiweishi.activity.acivitys.com.Constants;
import com.example.mybiadushoujiweishi.activity.acivitys.entity.BBX;
import com.example.mybiadushoujiweishi.activity.acivitys.entity.Contact;
import com.example.mybiadushoujiweishi.activity.db.BBXDao;
import com.example.mybiadushoujiweishi.activity.db.DaoMaster;
import com.example.mybiadushoujiweishi.activity.db.DaoSession;
import com.socks.library.KLog;
import com.zhy.http.okhttp.OkHttpUtils;


import org.greenrobot.greendao.database.Database;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

/**
 * Created by Administrator on 2017/4/20.
 */

public class MyApplication extends Application {
    static Context context;
    static DaoSession daoSession;
    static DaoSession mobiledaoSession;
    static List<Contact> list;
    static List<Contact> soslist = new ArrayList<Contact>();


    public static List<Contact> getSoslist() {


        return soslist;
    }


    public static List<Contact> getList() {
        return list;
    }


    @Override
    public void onCreate() {
        super.onCreate();
        initClient();
        context = getApplicationContext();
        KLog.init(BuildConfig.LOG_DEBUG, Constants.PRINTLOG);
        setupDataBase();
        initDatabase1();
        new Thread(new Runnable() {
            @Override
            public void run() {
                initDatatoTable();
                try {
                    Myuitl.copyFiletoMobile(context, "mobile.db", "mobile.db");
                    list = Myuitl.getAllContacts();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }


    //配置OKhttp
    public void initClient() {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
//                .addInterceptor(new LoggerInterceptor("TAG"))
                .connectTimeout(10000L, TimeUnit.MILLISECONDS)
                .readTimeout(10000L, TimeUnit.MILLISECONDS)
                //其他配置
                .build();

        OkHttpUtils.initClient(okHttpClient);
    }


    public static Context getContext() {
        return context;
    }

    public static SharedPreferences getSharedPreferences() {
        return MyApplication.getContext().getSharedPreferences("loginwel", Context.MODE_PRIVATE);
    }


    public static int getVersionCode(Context context)//获取版本号(内部识别号)
    {
        try {
            PackageInfo pi = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            return pi.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return 0;
        }
    }


    private void setupDataBase() {

        DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(this, Constants.DATA_BASE_NAME);
        Database database = openHelper.getWritableDb();
        DaoMaster daoMaster = new DaoMaster(database);
        daoSession = daoMaster.newSession();


    }


    public static DaoSession getDaoSession() {


        return daoSession;
    }


    private static void initDatabase1() {
        DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(context, "mobile.db");
        Database database = openHelper.getWritableDb();
        DaoMaster daoMaster = new DaoMaster(database);
        mobiledaoSession = daoMaster.newSession();
    }

    public static DaoSession getMobiledaoSession() {
        return mobiledaoSession;
    }


    public void initDatatoTable() {

//        Myuitl.getAllContacts();
        DaoSession daoSession = getDaoSession();
        BBXDao bbxDao = daoSession.getBBXDao();

    /*    try {
            Myuitl.copyFiletoMobile(context,"mobile.db","aaa.db");
        } catch (Exception e) {
            e.printStackTrace();
        }
*/
        //数据源在strings.xml里面;

        SharedPreferences sharedPreferences = MyApplication.getSharedPreferences();
        boolean b = sharedPreferences.getBoolean("insertable", false);

        if (!b) {

            String[] bbxname = getResources().getStringArray(R.array.bbx_name);
            String[] bbxico = getResources().getStringArray(R.array.bbx_icon);


            System.out.println(bbxname.length);

            //  BBX bbx=new BBX();


            for (int i = 0; i < bbxname.length; i++) {
                BBX bbx = new BBX(null, bbxname[i], bbxico[i]);

                long m = 0;
                try {
                    m = bbxDao.insert(bbx);
                    if (m > 0) {
                        KLog.i("====insert success");

                    } else {

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }


            }

            sharedPreferences.edit().putBoolean("insertable", true).commit();

        }
    }


}
